/*
 * CustomMessageDialog 自定义MessageDialog组件
 */
import QtQuick 2.14
import QtQuick.Layouts 1.14
import QtQuick.Controls 2.14
import QtGraphicalEffects 1.14

Item {
    id: root

    /* property 定义开始 */
    // 私有属性
    property int _radius: 13
    // 提示框信息
    property var _boxMessage: undefined  // 实际上是JSON字符串
    //                                      boxMessage {
    //                                         boxType: BoxType(enum),
    //                                         title: string,
    //                                         text: string,
    //                                         standardButton: StandardButton(enum)
    //                                      }
    // 提示框ICON
    property url _iconPath: ""
    // 提示框文字
    property string _text: "--BoxMsg Default--"
    // 提示框窗口类型对应图标映射
    property var _type2IconMapper: ["qrc:/icons/msgicon_tips.png"]
    // 按钮渲染
    property int _buttonNumber: 0 // 按位或得来的

    // 函数
    // 解析回传信息
    function analyseBoxMessage(boxMsg){
        if(boxMsg.length === 0){
            _text = "提示信息有误，请检查！"
        }else {
            console.log(boxMsg)
            var json = JSON.parse(boxMsg)
            _text = json["text"] // 提示框文字
            _iconPath = _type2IconMapper[json["boxType"]] // icon路径
            _buttonNumber = json["standardButton"]
        }
    }

    // 按钮处理
    function analyseButtonNumber(btnNum){
        if(btnNum === GLOBAL.StandardButton.NoButton){
            console.log("No Button")
        }
        if(btnNum & GLOBAL.StandardButton.Submit){
            console.log("Submit Button")
            createButton("MessageBoxButton.qml",
                         btnLayout,
                         {"Layout.preferredWidth": 105, "Layout.preferredHeight": 38, "msgBtnText": "确定"})
        }

        if(btnNum & GLOBAL.StandardButton.Cancel){
            console.log("Cancel Button")
            createButton("MessageBoxButton.qml",
                         btnLayout,
                         {"Layout.preferredWidth": 105, "Layout.preferredHeight": 38, "msgBtnText": "取消"})
        }

        if(btnNum & GLOBAL.StandardButton.Return){
            console.log("Return Button")
            createButton("MessageBoxButton.qml",
                         btnLayout,
                         {"Layout.preferredWidth": 105, "Layout.preferredHeight": 38, "msgBtnText": "返回"})
        }
    }

    // 按钮创建
    function createButton(qmlFileName, parent, propertyObj, eventList){
        var comp = Qt.createComponent(qmlFileName)
        if(comp.status === Component.Ready){
            var compObj = comp.createObject(parent, propertyObj)

        }
    }

    // c++信号的槽函数连接
    Connections {
        target: msgBox
        function onBoxMessageChanged(boxmsg){
            _boxMessage = boxmsg
            analyseBoxMessage(_boxMessage)
            analyseButtonNumber(_buttonNumber)
        }
    }
    /* property 定义结束 */

    Rectangle {
        anchors.fill: parent
        color: "transparent"
        radius: _radius

        // 阴影
        Rectangle {
            anchors.fill: parent
            color: "transparent"
            layer.enabled: true  // 设置layer为enable
            layer.effect: DropShadow {
                radius: 21
                samples: 43
                transparentBorder: true
                color: "#4D000000"
            }
        }

        ColumnLayout {
            anchors.fill: parent

            // 内容区
            Item {
                Layout.fillWidth: true
                Layout.fillHeight: true
                anchors.rightMargin: 21

                RowLayout {
                    id: contentLayout
                    anchors.fill: parent

                    Item {
                        Layout.preferredWidth: 52;
                        Layout.fillHeight: true;
                        Layout.leftMargin: 21
                        Layout.rightMargin: 21

                        // 图标
                        Image {
                            id: icon
                            width: 52
                            height: 52
                            anchors.centerIn: parent
                            source: _iconPath
                        }
                    }

                    // 内容
                    Label {
                        id: text
                        Layout.fillWidth: true
                        Layout.fillHeight: true
                        Layout.leftMargin: 4
                        Layout.rightMargin: 21
                        font.family: "Alibaba PuHuiTi"
                        font.pixelSize: 17
                        color: "#222222"
                        text: _text
                        verticalAlignment: Text.AlignVCenter
                        wrapMode: Text.WordWrap // 换行
                    }
                }
            }

            // 按钮区
            Rectangle {
                Layout.fillWidth: true
                Layout.preferredHeight: 73
                color: "#F4F4F5"

                RowLayout {
                    id: btnLayout
                    anchors.fill: parent
                    anchors.rightMargin: 21

                    // 按钮右对齐
                    CustomHorSpacerItem {}
                }
            }
        }
    }

}
